package arrayandstring;
/**
 * https://leetcode-cn.com/problems/slowest-key/
 */
public class E1629_SlowestKey {
    /**
     * (暴力解法)
     * 时间复杂度 O(n) 空间复杂度 O(1) 
     */
    public static char slowestKey(int[] releaseTimes, String keysPressed) {
        // 最长的持续时间
        int longesttime = releaseTimes[0];
        // 按键持续时间最长的键
        char target = keysPressed.charAt(0);

        for (int i = 1; i < releaseTimes.length; i++) {
            int time = releaseTimes[i] - releaseTimes[i-1];
            char key = keysPressed.charAt(i);
            // 按键持续时间相同按字母顺序靠后的优先级高
            if (longesttime < time || longesttime == time && target < key) {
                longesttime = time;
                target = key;
            }
        }
        return target;
    }
    public static void main(String[] args) {
        int[] releaseTimes = {9,29,49,50,70};
        String keysPressed = "cbcda";
        slowestKey(releaseTimes, keysPressed);
    }
}
